<?php
/**
 * @author Jefferson González
 * 
 * @license 
 * This file is part of wxPHP check the LICENSE file for information.
 * 
 * @description
 * Script to merge new changes done on the classes.json file generated 
 * by the json_generator on to the classes.json file used by the source code 
 * generator. This script was created after adding some new options to
 * the generated classes.json like platforms and parameters_is_array.
 * 
 * The script will generate a new classes.json on the tools directory as
 * a merge_changes.log file where you can check methods that were removed.
 * You should replace the classes.json file on the source_maker/class_selector
 * with the generated by this script and make manual changes if neccesary.
 */

//Disable anoying warnings and notices
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);

//Change to correct working directory to be able to execute the script from everywhere
chdir(__DIR__);

include("source_maker/include/functions.php");

if(!file_exists("../json/classes.json"))
{
	die("classes.json not found on json/");
}

$full_classes = unserialize_json(file_get_contents("../json/classes.json"));

$binded_classes = unserialize_json(file_get_contents("source_maker/class_selector/classes.json"));

$output_classes = array();


file_put_contents("merge_changes.log", "");


//First check if new special flags where added like _platforms to the class definition

file_put_contents("merge_changes.log", "New class flags merged\n\n", FILE_APPEND);

foreach($binded_classes as $class_name=>$class_methods)
{
	foreach($full_classes[$class_name] as $full_method_name=>$full_method_data)
	{
		if($full_method_name{0} == "_" && $full_method_name != "_implements")
		{
			if(!in_array($full_method_name, $class_methods))
			{	
				file_put_contents("merge_changes.log", "Merged the class flag $full_method_name into $class_name\n", FILE_APPEND);
			}
			
			$binded_classes[$class_name][$full_method_name] = $full_method_data;
		}
	}
}

file_put_contents("merge_changes.log", "\n\n", FILE_APPEND);


//Append new special flags added like parameters_extra to method definition
//and remove method overloads not found on full version of classes.json

file_put_contents("merge_changes.log", "Removed method overloads\n\n", FILE_APPEND);

foreach($binded_classes as $class_name=>$class_methods)
{
	foreach($class_methods as $method_name=>$method_definitions)
	{
		//Skip class special property flags
		if($method_name{0} == "_")
		{
			continue;
		}
		
		$temp_method_overloads = array();
		
		foreach($method_definitions as $method_index=>$method_definition)
		{
			$found_full_method = false;
			$found_full_method_index = 0;
			
			foreach($full_classes[$class_name][$method_name] as $full_method_index=>$full_method_definition)
			{				
				if(($method_definition["return_type"] == $full_method_definition["return_type"]) &&
					($method_definition["constant"] == $full_method_definition["constant"]) &&
					($method_definition["virtual"] == $full_method_definition["virtual"]) &&
					($method_definition["pure_virtual"] == $full_method_definition["pure_virtual"]) &&
					($method_definition["static"] == $full_method_definition["static"]) &&
					($method_definition["protected"] == $full_method_definition["protected"]) &&
					($method_definition["parameters_name"] == $full_method_definition["parameters_name"]) &&
					($method_definition["parameters_type"] == $full_method_definition["parameters_type"]) &&
					($method_definition["parameters_required"] == $full_method_definition["parameters_required"]) &&
					($method_definition["parameters_default_value"] == $full_method_definition["parameters_default_value"]))
				{
					$found_full_method = true;
					$found_full_method_index = $full_method_index;
					break;
				}
			}
			
			if(!$found_full_method)
			{
				file_put_contents("merge_changes.log", $class_name . "::" . $method_name . " overload $method_index" . "\n", FILE_APPEND);
			}
			else
			{
				$temp_method_overloads[] = $full_classes[$class_name][$method_name][$found_full_method_index];
			}
		}
		
		if(count($temp_method_overloads) > 0)
		{
			$binded_classes[$class_name][$method_name] = $temp_method_overloads;
		}
		else
		{
			unset($binded_classes[$class_name][$method_name]);
		}
	}
}

file_put_contents("merge_changes.log", "\n", FILE_APPEND);


//Save new classes.json output
file_put_contents("classes.json", serialize_json($binded_classes));

?>
